#include "btn_Impl.h"
#include "ttServer.h"
#include "ttClient.h"
#include "myUtil.h"
#include "btnManager.h"
#include "editManager.h"
#include "ttMsgMap.h"
#include "winProcCallBacks.h"
#include "Me.h"
#include "timerManager.h"
#include "timer_Impl.h"
#include "ttClientPrxMgr.h"
#include "d3dDrawer.h"

#include <atlconv.h>


SETSHAPE(createHost, 100, 100, 200, 100)
SETSHAPE(joinGame, 400, 100, 200, 100)

int InitTimer(HWND hwnd);

int 
createHost::OnButtonClick()
{
	myUtil::myCreateThread(ttServer::listenThread);	
	
	Me = ServerMe::Get();
	WCHAR wname[64];
	GetWindowText(editManager::Get()->getObj(1)->getHwnd(), wname, 64);
	Me->setName(wname);
	InvalidateRect(this->m_hWndContainer, NULL, FALSE);
	InitTimer(this->m_hWndContainer);
	btnManager::Get()->unload();
	editManager::Get()->unload();
	Me->setID(IDMaker::Get());

	ttMsgMap::Get()->m_msglpvar.m_bMessaging = false;
	ttMsgMap::Get()->push_front(new onKeyDown);
	ttMsgMap::Get()->push_front(new onKeyUp);
	ttMsgMap::Get()->push_front(new onMouseMove);

	d3dDrawer::Get()->begin();
    myUtil::midCursor(0, 0);
    ShowCursor(FALSE);
	return 0;
}

int
joinGame::OnButtonClick()
{
	WCHAR wip[64], wname[64];
	GetWindowText(editManager::Get()->getObj(0)->getHwnd(), wip, 64);
	GetWindowText(editManager::Get()->getObj(1)->getHwnd(), wname, 64);
	USES_CONVERSION;
	char *ip = W2A(wip);
	ttClient::Get()->setIPandPortandName(ip, 4999, wname);
	ttClient::Get()->ttConnect();
	
	Me = ttClient::Get();
	InvalidateRect(this->m_hWndContainer, NULL, FALSE);
	InitTimer(this->m_hWndContainer);
	editManager::Get()->unload();
	btnManager::Get()->unload();

	ttMsgMap::Get()->m_msglpvar.m_bMessaging = false;
	ttMsgMap::Get()->push_front(new onKeyDown);
	ttMsgMap::Get()->push_front(new onKeyUp);
	ttMsgMap::Get()->push_front(new onMouseMove);

	d3dDrawer::Get()->begin();
    myUtil::midCursor(0, 0);
    ShowCursor(FALSE);
	return 0;
}


int InitTimer(HWND hwnd)
{
	//timerManager::Get()->setTimer(hwnd, 1000, new testtimer);
	timerManager::Get()->setTimer(hwnd, 1, new painttimer);
	return 0;
}